package in.abacritt.android.xclock;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.widget.RemoteViews;

public class XClockWidgetUpdater {
	RemoteViews widgetView;
	Context context;
	AppWidgetManager appWidgetManager;
	ComponentName thisWidget;
	WeatherInfo weatherInfo;

	public XClockWidgetUpdater(Context context) {
		appWidgetManager = AppWidgetManager.getInstance(context);

		weatherInfo = null;

		this.context = context;

		widgetView = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);

		thisWidget = new ComponentName(context, XClockWidgetProvider.class);
	}

	public WeatherInfo getWeatherInfo() {
		return weatherInfo;
	}

	public void setWeatherInfo(WeatherInfo weatherInfo) {
		this.weatherInfo = weatherInfo;
	}

	public synchronized void runAndUpdate(boolean updateWeather) {

		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

		for (final int appWidgetId : allWidgetIds) {
			// Get date time
			updateDateTime();
			if (updateWeather == true) {
				updateWeather();
			}
			appWidgetManager.updateAppWidget(appWidgetId, widgetView);
		}
	}

	private void updateDateTime() {
		String dateFormat = "EEEE, dd MMM  yyyy";

		Calendar currentDateTime = Calendar.getInstance();

		int hourNum = currentDateTime.get(Calendar.HOUR_OF_DAY);
		if (hourNum > 12)
			hourNum = hourNum - 12;
		else if (hourNum == 0)
			hourNum = 12;

		String hour = Integer.toString(hourNum);
		String min = Integer.toString(currentDateTime.get(Calendar.MINUTE));
		if (min.length() == 1)
			min = "0" + min;

		int ampm = currentDateTime.get(Calendar.AM_PM);

		// View population
		widgetView.setTextViewText(R.id.tv_widget_time_hour, hour);
		widgetView.setTextViewText(R.id.tv_widget_time_min, min);
		if (ampm == Calendar.AM) {
			widgetView.setTextViewText(R.id.tv_widget_ampm,
					context.getString(R.string.widget_am));
		} else {
			widgetView.setTextViewText(R.id.tv_widget_ampm,
					context.getString(R.string.widget_pm));
		}

		SimpleDateFormat sdm = new SimpleDateFormat(dateFormat);
		widgetView.setTextViewText(R.id.tv_widget_date_text,
				sdm.format(currentDateTime.getTime()));
	}

	private void updateWeather() {
		// weather update code
		// location
		widgetView.setTextViewText(R.id.tv_weather_location,
				weatherInfo.locationName);

		// icon
		widgetView.setImageViewResource(
				R.id.iv_weather_cond,
				context.getResources().getIdentifier(weatherInfo.iconName,
						"drawable", context.getPackageName()));

		Log.d("weathericon",
				weatherInfo.iconName + ";" + context.getPackageName());
		// temp & condition
		widgetView.setTextViewText(R.id.tv_weather_temp,
				weatherInfo.temperature.substring(0, 2)
						+ OpenWeatherConfig.degreeSign
						+ weatherInfo.tempUnit.toUpperCase().charAt(0) + "  "
						+ weatherInfo.conditionText);

		Log.d("weatherupdate", "Weather updated successfully!");
	}
}
